package com.jieli.bt.att.ui.settings

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import com.jieli.bt.att.R
import com.jieli.bt.att.databinding.FragmentSettingsBinding
import com.jieli.bt.att.ui.common.CommonActivity
import com.jieli.bt.att.ui.common.basic.BasicFragment
import com.jieli.bt.att.ui.settings.log.LogFileFragment
import com.jieli.bt.att.util.BluetoothUtil
import com.jieli.bt.att.util.ViewUtil
import com.jieli.bt.att.util.gone
import com.jieli.bt.att.util.show
import com.jieli.logcat.JL_Log

/**
 * @author zqjasonZhong
 * @since 2025/3/27
 * @email zhongzhuocheng@zh-jieli.com
 * @desc 设置页面
 */
class SettingsFragment : BasicFragment() {

    companion object {
        fun newInstance() = SettingsFragment()
    }

    private val viewModel: SettingsViewModel by viewModels()
    private lateinit var binding: FragmentSettingsBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        FragmentSettingsBinding.inflate(inflater, container, false).also {
            binding = it
            return it.root
        }
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initUI()
    }

    private fun initUI() {
        binding.viewTopBar.tvTitle.text = getString(R.string.tab_settings)

        binding.viewLogcatPath.root.apply {
            if (JL_Log.isLog()) {
                binding.tvLogcatInfo.show()
                show()
                val logcatPath = JL_Log.getLogOption().logFileOption.logFileDirPath.replace("/storage/emulated/0", "")
                ViewUtil.updateSettingText(
                    binding.viewLogcatPath,
                    getString(R.string.logcat_save_path),
                    logcatPath, true
                )
            } else {
                binding.tvLogcatInfo.gone()
                gone()
            }
            setOnClickListener {
                //进入日志处理界面
                CommonActivity.startCommonActivity(
                    requireContext(),
                    LogFileFragment::class.java.canonicalName
                )
            }
        }

        val appVersionStr = BluetoothUtil.formatString(
            "%s(%d)", ViewUtil.getAppVersionName(requireContext()),
            ViewUtil.getAppVersion(requireContext())
        )
        ViewUtil.updateSettingText(
            binding.viewAppVersion,
            getString(R.string.app_version),
            appVersionStr
        )
    }
}